package com.fresh.bus.chat.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;

import com.fresh.bus.canoe.command.annotation.CanoeCommand;
import com.fresh.bus.canoe.message.Request;
import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Role;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.chat.business.MessageService;
import com.fresh.bus.chat.domain.User;

@Controller("chatCtrl")
public class ChatCtrl extends BaseCtrl {

	@CanoeCommand(value = OpCodes.REQ_TALK)
	public Response reqTalk(Session session, Request request) {
		try {
			Map<String, Object> data = (Map<String, Object>) request.getData();
			String id = session.getRole().getId();

			String to = (String) data.get("u");
			String words = (String) data.get("w");
			int flowerAmt = (Integer) data.get("f");

			Response response = new Response(OpCodes.REQ_TALK);
			Session toSession = this.roleManager.getSession(to);
			response.setData(this.talkService.requestTalk(id,to,words, flowerAmt));
			toSession.send(response);
		} catch (Exception ex) {
			log.error("Some error occur...", ex);
		}
		return null;
	}

	@CanoeCommand(value = OpCodes.REJECT_TALK)
	public Response rejectTalk(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		String user = String.valueOf(data.get("u"));

		try {
			Response response = new Response(OpCodes.REJECT_TALK);
			// this.msgService.notifyTargetUser(user,
			// MessageService.REJECT_TALK,id);
			Session toSession = this.roleManager.getSession(user);
			response.setData(true);
			if(toSession!=null)toSession.send(response);
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.TALK)
	public Response responseChat(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		data.put("from", ((User) session.getRole()).getId());

		Response response = new Response(OpCodes.TALK);
		// service.getUserById("tom");
		response.setData(data);
		if (session.getAttribute("to") != null) {
			String to = (String) session.getAttribute("to");
			List<Role> recievers = new ArrayList<Role>();
			// recievers.add(session.getRole());
			recievers.add(roleManager.getRoleById(to));
			response.setRecievers(recievers);
			return response;
		}
		return null;
	}

	@CanoeCommand(value = OpCodes.START_TALK)
	public Response startTalk(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		try {
			String id = session.getRole().getId();
			String to = String.valueOf(data.get("u"));
			String rid = (String) session.getAttribute("rid");
			
			Response response = new Response(OpCodes.START_TALK);
			if (session.getAttribute("isTalking") != null) {
				response.setData("亲，您正在谈话状态，不能再接受他人的谈话申请。");
				return response;
			}
			Session toSession = this.roleManager.getSession(to);
			if (toSession == null) {
				response.setData("对方已离开。");
				return response;
			}
			if (toSession.getAttribute("isTalking") != null) {
				response.setData("对方正在谈话状态。");
				return response;
			}
			Integer flower = (Integer) data.get("f");
			if (flower > 0) {
				boolean flag = wealthService.giveFlower(to,id,flower);
				if(!flag) throw new Exception("玫瑰数量不足");
			}
			talkService.startTalk(id,to);
		} catch (Exception ex) {
			log.error("Some error occur...", ex);
		}
		return null;
	}

	@CanoeCommand(value = OpCodes.END_TALK)
	public Response endTalk(Session session, Request request) {
		if (session.getAttribute("isTalking") != null) {
			talkService.endTalk(session, true);
		}
		return null;
	}

	@CanoeCommand(value = OpCodes.SEND_FLOWER)
	public Response sendFlower(Session session, Request request) {
		Response response = new Response(OpCodes.SEND_FLOWER);
		String id = session.getRole().getId();
		String to = (String) session.getAttribute("to");
		String rid = (String) session.getAttribute("rid");

		try {
			boolean flag = wealthService.giveFlower(id, to, 1);
			if(!flag) throw new Exception("玫瑰数量不足");
			// userManager.u
			//this.assoService.update(id, to, AssoValueService);

			Response res2 = new Response(OpCodes.GET_FLOWER);
			res2.setData(true);
			this.roleManager.getSession(to).send(res2);

			Map<String, String> data = new HashMap<String, String>();
			data.put("id", to);
			response.setData(data);
		} catch (Exception e) {
			log.error("Login failed...", e);
			response.setData(e.getMessage());
		}
		return response;
	}
}
